from django.contrib.auth.models import AbstractUser
from django.db import models
from datetime import datetime


class UserProfile(AbstractUser):
    """
    用户信息表,默认继承系统User用户表
    """
    nickName = models.CharField(verbose_name=u"昵称", max_length=50, default="慕课会员")
    mobilePhone = models.CharField(verbose_name=u"手机号码", max_length=11, null=True, blank=True)
    birthday = models.DateField(verbose_name=u"生日", null=True, blank=True)
    sex = models.CharField(verbose_name=u"性别", max_length=5, choices=(("male", "男"), ("female", "女")), default="male")
    address = models.CharField(verbose_name=u"地址", max_length=100, default=u"")
    image = models.ImageField(verbose_name=u"头像", max_length=100, upload_to="HeadPortrait/user/%Y/%m/%d",default=u"HeadPortrait/default.png", null=True, blank=True)

    class Meta:
        verbose_name = u"用户信息"
        verbose_name_plural = verbose_name
        # db_table = "usermessage"
        # ordering = "-id"

    def __str__(self):
        return self.username

    # 获取用户未读消息
    def getUnReadMessages(self):
        # from operations.models import UserMessage
        # from django.db.models import Q
        # return UserMessage.objects.filter(user=self.id, hasRead=False).count()
        return 1


class EmailVerifyRecord(models.Model):
    """
    邮箱验证表：
    """
    code = models.CharField(verbose_name=u"验证码", max_length=20)
    email = models.EmailField(verbose_name=u"邮箱", max_length=50)
    sendType = models.CharField(verbose_name=u"验证类型", max_length=20,choices=(("register", "注册"), ("forget", "找回密码"), ("modifyEmail", "修改邮箱")))
    # datetime.now class实例化的时间,datetime.now()class编译的时间
    sendTime = models.DateTimeField(verbose_name=u"发送时间", default=datetime.now)

    class Meta:
        verbose_name = u"邮箱验验信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "{0}({1})".format(self.code, self.email)


class Banner(models.Model):
    """
    首页轮播图
    """
    title = models.CharField(verbose_name=u"标题", max_length=100)
    image = models.ImageField(verbose_name=u"轮播图", max_length=100, upload_to="banner/%Y/%m/%d")
    url = models.URLField(verbose_name=u"图片访问链接", max_length=200)
    index = models.IntegerField(verbose_name=u"图片轮播顺序", default=100)
    addTime = models.DateTimeField(verbose_name=u"添加时间", default=datetime.now)

    class Meta:
        verbose_name = u"轮播图信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "{0}({1})".format(self.title, self.addTime)

    def showBannerInfo(self):
        from django.utils.safestring import mark_safe
        return mark_safe("<a href='" + self.url + "'>链接</a>")


    showBannerInfo.short_description = "图片信息"